/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.util.io;

import co.edu.unal.bioingenium.kbmed.util.xml.DocumentXML;
import co.edu.unal.bioingenium.kbmed.util.xml.XMLDOM;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author lariverosc
 */
public class StoreDocument {

    //TODO modify to manage general XML files add parameter filename
    /**
     * 
     * @param path
     * @param documentXML
     */
    public static void saveXMLFile(String path, DocumentXML documentXML) {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.newDocument();
            Element element = document.createElement(documentXML.getRoot().getName());
            XMLDOM.convertToDOMXML(document, element, documentXML.getRoot());
            document.appendChild(element);
            Source source = new DOMSource(document);
            Result result = new StreamResult(new File(path + documentXML.getNode("caso" + DocumentXML.XML_PATH_SEPARATOR + "id").getValue() + ".xml").toURI().getPath());
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.transform(source, result);
        } catch (ParserConfigurationException pce) {
            Logger.getLogger(XMLDOM.class.getName()).log(Level.SEVERE, null, pce);
        } catch (TransformerConfigurationException tce) {
            Logger.getLogger(XMLDOM.class.getName()).log(Level.SEVERE, null, tce);
        } catch (TransformerException te) {
            Logger.getLogger(XMLDOM.class.getName()).log(Level.SEVERE, null, te);
        }
    }

    /**
     * 
     * @param path
     * @param documentXML
     */
    public static void saveTXTFile(String path, DocumentXML documentXML) {
        try {
            File file = new File(path + documentXML.getNode("caso" + DocumentXML.XML_PATH_SEPARATOR + "id").getValue() + ".txt");
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(documentXML.toString());
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(StoreDocument.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
